package com.redis.dao.impl;

import com.redis.dao.CommonRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

/**
 * Created by junli on 2018/1/11.
 */
@Repository
public class CommonRedisDaoImpl implements CommonRedisDao {

    private final RedisTemplate<String, String> redisTemplate;

    @Autowired
    public CommonRedisDaoImpl(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public boolean cacheStringValue(String key, String value) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        try {
            opsForValue.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public String getStringValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}
